package com.itshi.plic;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author げっしょく
 * @date 2021/8/19
 */
public class Test2 {
    public static void main(String[] args) throws Exception {
        /*
        2. 创建表，用【JDBC】完成下列需求：
	create table account (
		id int primary key auto_increment,
		name varchar(20), -- 账户名
		money int -- 账户余额
	)

	1. 给表添加三条数据，账户名分别为张三、李四、老王
	2. 将所有账户的金额都修改成1000
	3. 删除账户名为老王的数据
	4. 模拟一次转账操作，用事务管理起来：
		张三给李四转500元
         */

        //Class.forName("com.mysql.jdbc.Driver");
        //Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/song","root","1234");
        //String sql1 = "insert into account values(0,'张三','1500')";
        //String sql2 = "insert into account values(0,'李四','10000')";
        //String sql3 = "insert into account values(0,'老王','500')";
        //Statement sta = con.createStatement();
        //int i = sta.executeUpdate(sql1);
        //int i1 = sta.executeUpdate(sql2);
        //int i2 = sta.executeUpdate(sql3);
        //System.out.println(i);
        //String sql = "update account set money=1000 ";
        //Statement statement = con.createStatement();
        //int i = statement.executeUpdate(sql);
        //System.out.println(i);
        //String sql = "delete from account where name='老王'";
        //Statement sta = con.createStatement();
        //int i = sta.executeUpdate(sql);
        //System.out.println(i);
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/song","root","1234");
        String sql1 = "update account set money=money-500 where name='张三'";
        String sql2 = "update account set money=money+500 where name='李四'";
        Statement statement = con.createStatement();
        try{
            con.setAutoCommit(false);
            int i = statement.executeUpdate(sql1);
            int i1 = statement.executeUpdate(sql2);
            con.commit();
        } catch (SQLException e) {
            con.rollback();
            e.printStackTrace();
        }
        con.close();
        statement.close();



    }
}
